package com.google.code.burner.pomodoro;

import java.util.Calendar;
import java.util.Date;
import java.util.TimerTask;

public class Task extends TimerTask {

	private SimplePomodoroTimer parent;
	private int duration;
	private Date end;
	private long remaining;

	public Task(SimplePomodoroTimer parent, int minutes) {
		this.parent = parent;
		this.duration = minutes;

		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		cal.add(Calendar.MINUTE, minutes);
		this.end = cal.getTime();

	}

	public int getDuration() {
		return duration;
	}

	@Override
	public void run() {
		Date now = new Date();
		if(this.end.before(now)) {
			cancel(); 
			parent.alert("Zeit ist abgelaufen!");
			parent.setActive(false);
		}
		this.remaining = end.getTime()-now.getTime();
		parent.updateRemainingTimeLabel(getRemaining());
	}

	public long getRemaining() {
		return this.remaining;
	}
}
